package com.example.easyscript.service.task;

import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.easyscript.bean.task.OperationBean;
import com.example.easyscript.bean.task.StepBean;
import com.example.easyscript.entity.task.Operation;
import com.example.easyscript.enums.OperationStatusEnum;
import com.example.easyscript.mapper.OperationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author huzixian
* @date  2023-03-08
*/
@Slf4j
@Service
public class OperationService extends ServiceImpl<OperationMapper, Operation>{

    @Resource
    private StepService stepService;

    public Map<Integer, OperationBean> getOperationBeanMapByTaskId(Integer taskId) {
        List<Operation> operationList = TaskLoader.loadEntityListByTaskId(taskId, this, Operation.class);
        Map<Integer, OperationBean> map = operationList.stream().map(item -> {
            OperationBean operationBean = CglibUtil.copy(item,OperationBean.class);
            Map<Integer, StepBean> stepMap = stepService.getStepMapByOperationId(item.getId());
            operationBean.setStepBeanMap(stepMap);
            operationBean.setStatusEnum(OperationStatusEnum.init);
            return operationBean;
        }).collect(Collectors.toMap(OperationBean::getOrderId,v->v));
        return map;
    }
}
